package com.xjj.service.health.controller.api;

import com.xjj.entity.Health;
import com.xjj.entity.Leave;
import com.xjj.entity.ResponseEntity;
import com.xjj.service.health.service.HealthService;
import com.xjj.service.health.vo.HealthSubmit;
import com.xjj.util.utils.OssManagerUtil;
import com.xjj.util.utils.RandomUtil;
import com.xjj.vo.HealthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: AYang
 * @Date: 2022/10/28 09:40
 * @Description:
 */
@Api(tags = "健康管理相关接口")
@RestController
@RequestMapping("/health/api")
@Slf4j
public class ApiHealthController {
    @Autowired
    private HealthService healthService;
    // 健康提醒列表
    @GetMapping("/getByStudent/{studentId}")
    public ResponseEntity<List<Health>> getByStudent(@PathVariable("studentId") Integer studentId, String status) {
        healthService.getByStudent(studentId,status);
        return ResponseEntity.SUCCESS("ok", healthService.getByStudent(studentId, status));
    }

    @RequestMapping("/upload")
    public ResponseEntity uploadImg(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return ResponseEntity.FAIL("文件找不到", null);
        }
        // 获取图片后缀
        String submittedFileName = file.getOriginalFilename();
        String suffix = submittedFileName.substring(submittedFileName.lastIndexOf("."));
        // 图片随机前缀
        String fileName = RandomUtil.getImgRandom();
        // 拼接文件名
        String url = OssManagerUtil.uploadImage(fileName + suffix, file.getSize(), file.getInputStream());
        return ResponseEntity.SUCCESS("上传成功", url);
    }

    // 家长发起提醒
    @ApiOperation("请假申请")
    @PostMapping
    public ResponseEntity<Integer> addHealth(@RequestBody Health health) {

        Integer id = healthService.addHealth(health);

        return ResponseEntity.SUCCESS("ok", id);
    }
}
